﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AgileBoard.Domain;

namespace AgileBoard.Demo
{
    public class FakeLabelManager
    {
        private readonly List<Label> labelList;

        public FakeLabelManager(List<Label> labels)
        {
            labelList = labels;
        }

        /// <summary>
        /// Create a label (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Label NewLabel()
        {
            var label = new Label();

            label.CreateTime = DateTime.Now;
            label.Date = DateTime.Now.ToShortDateString();

            return label;
        }

        /// <summary>
        /// Save label
        /// </summary>
        /// <param name="label">Label entity</param>
        /// <returns></returns>
        public Label Save(Label label)
        {
            Validate(label);
            if (Get(label.LabelID) != null)
                throw new Exception("Existing label");
            label.LabelID = Guid.NewGuid();
            labelList.Add(label);

            return Get(label.LabelID);
        }

        /// <summary>
        /// Access to labels
        /// </summary>
        /// <param name="labelId">Label ID</param>
        /// <returns></returns>
        public Label Get(Guid labelId)
        {
            return labelList.SingleOrDefault(u => u.LabelID == labelId);
        }

        /// <summary>
        /// Access to labels
        /// </summary>
        /// <param name="labelId">Label ID</param>
        /// <returns></returns>
        public IList GetByIteration(string iterationId)
        {
            return labelList.FindAll(f => f.IterationID == iterationId);
        }

        /// <summary>
        /// Delete Label
        /// </summary>
        /// <param name="labelId">Label ID</param>
        /// <returns></returns>
        public void Delete(Guid labelId)
        {
            Label label = Get(labelId);
            labelList.Remove(label);
        }

        /// <summary>
        /// Update Label
        /// </summary>
        /// <param name="label">Label Entity</param>
        /// <returns></returns>
        public Label Update(Label label)
        {
            return Get(label.LabelID);
        }

        /// <summary>
        /// Access to labels
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return labelList;
        }

        private void Validate(Label label)
        {
            //TODO throw exception on error.
        }
    }
}